package naga.x.platform2.channel.weixin.support;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

import org.jdom2.Document;
import org.jdom2.Element;

import naga.x.platform2.app.Channel;
import naga.x.platform2.support.RequestPath;
import naga.x.platform2.utils.CommonUtils;
import naga.x.platform2.utils.StringUtils;


/**
 * 微信
 * @see <a href="http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html">网页授权获取用户基本信息</a>
 * @see <a href="https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_1">公众号支付</a>
 * @author yang.li
 *
 */
public class WeiXinApi {

	public static /*final*/ String APP_ID = "wxd5c0c36cc7775667";
	public static /*final*/ String MCHID = "1284993901"; // 商户号
	public static /*final*/ String KEY = "1b56e7cfca3ec6cf897ccf52599268d9"; // 商户支付密钥
//	public static final String APP_SECRET = "ee0c4a2fc0af152cb5d93d2b99ebaef0"; // 公众帐号secert
	
	public static final String URL_GET_ORDER = "https://api.mch.weixin.qq.com/pay/unifiedorder";
	public static final String URL_AUTH = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type={2}&scope={3}&state={4}#wechat_redirect";
	public static final String URL_ACCESS_TOKEN = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code";
	public static final String URL_USER_INFO = "https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN";

//	public static final String IP = "pay.xtoneapp.com";
//	public static final String IP = "h5.xtoneapp.com";
	public static /*final*/ String URL_LOGIN_REDIRECT;// = "http://" + IP + RequestPath.WX_LOGIN_CALLBACK;
	public static /*final*/ String URL_PAY_CALLBACK;// = "http://" + IP + RequestPath.WX_PAY_CALLBACK;
	public static /*final*/ String URL_GAME;// = "http://city.arkfel.com?openid={0}&nickname={1}&sex={2,number,#}&province={3}&city={4}&headimgurl={5}&channel=2";
	
	public static final String RESPONSE_SUCC = "SUCCESS";
	public static final String RESPONSE_FAIL = "FAIL";
	
	public static void init(WeiXinConfig config) {
		APP_ID = config.getAppId();
		MCHID = config.getMchId();
		KEY = config.getKey();
		URL_LOGIN_REDIRECT = "http://" + config.getDomain() + RequestPath.WX_LOGIN_CALLBACK;
		URL_PAY_CALLBACK = "http://" + config.getDomain() + RequestPath.WX_PAY_CALLBACK;
		URL_GAME = "http://" + config.getClientHost() + "?openid={0}&nickname={1}&sex={2,number,#}&province={3}&city={4}&headimgurl={5}&channel=" + Channel.WEIXIN.ordinal();
	}
	
	public static String makeSign(Map<String, String> map) {
		Map<String, String> tree = map instanceof TreeMap ? map : new TreeMap<String, String>(map);
		StringBuilder buf = new StringBuilder();
		for (Entry<String, String> entry : tree.entrySet()) {
			if (StringUtils.isNotEmpty(entry.getValue())) {
				buf.append(entry.getKey()).append('=').append(entry.getValue()).append('&');
			}
		}
		buf.append("key=").append(KEY);
		return CommonUtils.md5(buf.toString()).toUpperCase();
	}
	
	public static String makeSign(Document doc) {
		Element root = doc.getRootElement();
		List<Element> list = root.getChildren();
		Map<String, String> tree = new TreeMap<String, String>();
		for (int i = 0, size = list.size(); i < size; i++) {
			Element ele = list.get(i);
			if (!"sign".equals(ele.getName())) {
				tree.put(ele.getName(), ele.getText());
			}
		}
		return makeSign(tree);
	}
	
	public static String makeSign(Object... keyAndVals) {
		Map<String, String> tree = new TreeMap<String, String>();
		for (int i = 0, len = keyAndVals.length; i < len; i += 2) {
			String key = keyAndVals[i].toString();
			String val = keyAndVals[i + 1].toString();
			if (!"sign".equals(keyAndVals[i])) {
				tree.put(key, val);
			}
		}
		return makeSign(tree);
	}
	
}
